$(function () {
    getProductTypeOption();
    getUserTypeOption();
    getProduct();
    $("#name").change(function () {
        if ($("#name").val()!="")
            $("#name_tips").html("");
    });
    $("#name").blur(function () {
        if ($("#name").val()=="")
            $("#name_tips").html("请输入产品名称");
    });
});
function getProduct() {
    let id = sessionStorage.getItem("productid");
    $.ajax({
        url: '/back/product/info',
        type: 'get',
        data:{'id':id},
        dataType: 'json',
        success: function (data) {
            console.log(data.product.userType.name);
            $("#name").val(data.product.name);
            $("#user_type").find("option[value="+data.product.userType.id+"]").attr("selected",true);
            $("#product_type").find("option[value="+data.product.productType.id+"]").attr("selected",true);
            $("#price").val(data.product.price);
            $("#productimg").attr("src",data.product.photo);
        }
    })
}

function productEdit() {
    let photo = sessionStorage.getItem("productphoto");
    let data = {
        'id':sessionStorage.getItem("productid"),
        'name':$("#name").val(),
        'user_type':$("#user_type").find("option:selected").val(),
        'product_type':$("#product_type").find("option:selected").val(),
        'price':$("#price").val(),
        'photo':photo
    };
    console.log(photo);
    console.log(sessionStorage.getItem("productid"));
    if (photo!=""&&flag&&photo!=null&&$("#name").val()!=null&&$("#name").val()!="") {
        $.ajax({
            url: '/back/product/manage',
            type: 'post',
            data: data,
            dataType: 'text',
            success: function (data) {
                console.log(data);
                if (data == 1) {
                    alert("修改成功！");
                    $(".right").load("../html/back/product/product.html");
                } else if (data == 0) {
                    alert("修改失败！");
                }
            }
        });
    }else if (photo=="")
        alert("请选择产品图片！");
    else
        alert("请完善产品修改信息！");
}

function edit() {
    let formData = new FormData();
    formData.append('name',$("#name").val());
    formData.append('user_type',$("#user_type").find("option:selected").val());
    formData.append('product_type',$("#product_type").find("option:selected").val());
    formData.append('price',$("#price").val());
    formData.append('photo',$("#productphoto")[0].files[0]);
    $.ajax({
        url:'/back/product/manage',
        type:'post',
        data:formData,
        dataType:'text',
        success:function (data) {
            console.log(data);
            if (data==1){
                alert("修改成功！");
                $(".right").load("../html/back/product/product.html");
            }else if (data==0){
                alert("修改失败！");
            }
        }
    })
}

function pictureChange() {
    let formData = new FormData();//获取表单中的文件
    let productphoto = $('#productphoto')[0].files[0];
    if (productphoto!=null && productphoto!=undefined){
        formData.append("photo",productphoto);
        $.ajax({
            url: "/upload",
            type: 'post',
            data: formData,
            dataType: 'text',
            processData: false,
            contentType: false,
            success: function (data) {
                console.log(data);
                if (data != "") {
                    $("#productimg").attr("src", data);
                    sessionStorage.setItem("productphoto", data);
                } else if (data == 0) {
                    console.log("图片上传失败！");
                }
            },
            error: function () {
                alert("图片上传异常");
            }
        })
    }
}

function getUserTypeOption() {
    $.ajax({
        url: '/back/product/usertype',
        type: 'get',
        dataType: 'json',
        success: function (data) {
            $("#user_type").html("");
            $.each(data.userType,function (i,usertype) {
                $("#user_type").append("<option value='"+ usertype.id +"'>"+ usertype.name +"</option>");
            })
        }
    })
}

function getProductTypeOption() {
    $.ajax({
        url: '/back/product/type',
        type: 'get',
        dataType: 'json',
        success: function (data) {
            $("#product_type").html("");
            $.each(data.productType,function (i,producttype) {
                $("#product_type").append("<option value='"+ producttype.id +"'>"+ producttype.name +"</option>");
            })
        }
    })
}